﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Komunikator.Core
{
    class HttpConnector
    {
        //adresy serwerow, portow, numer gg i haslo aktualnego uzytkownika
        private StringBuilder message;
        public static string ggServerAddress { get; set; }
        public static int ggServerPort { get; set; }
        public static byte[] ggServerAddressBytes { get; set; }
        public static string ggPassword { get; set; }
        public static int ggNumber { get; set; }

        public void CreateMessage(int GGnumber) // przaed polaczenie - ustala adresy serwera do polaczenia
        {
            Stream responseStream;
            StreamReader responseStreamReader;
            string[] responseAddresses;
            string[] responseAddressAndPort;
            message = new StringBuilder();
            message.Append("/appsvc/appmsg_ver8.asp?fmnumber=");
            message.Append(GGnumber);
            message.Append("&version=8.0.0.7669");

            
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://appmsg.gadu-gadu.pl"+message.ToString());
            req.Method = "GET";
            req.ProtocolVersion = HttpVersion.Version11;

            WebResponse resp = req.GetResponse();
            responseStream = resp.GetResponseStream();
            responseStreamReader = new StreamReader(responseStream);
            responseAddresses = responseStreamReader.ReadToEnd().Split(' ');

            ggServerAddress = responseAddresses[3].Split('\n')[0];
            //ggServerAddress.Remove(ggServerAddress.Length-1);

            responseAddressAndPort = responseAddresses[2].Split(':');
            ggServerPort = 8074;//Int32.Parse(responseAddressAndPort[1]);

            responseStreamReader.Close();
            responseStream.Close();
            

        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    }
}
